<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@page import="java.io.InputStreamReader"%>
<%@page import="java.io.BufferedReader"%>
<%@page import="java.io.FileReader"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.io.UnsupportedEncodingException"%>
<%@page import="java.io.File"%>
<%
	String strDir = request.getParameter("path");//文件路径
	String error = "";//错误记录

	if (strDir != null && !"".equals(strDir)) {

		strDir = URLDecoder.decode(strDir, "UTF-8");//路径解码
		File file = new File(strDir);//读取文件

		if (file.exists() && file.isFile()) {//文件存在并且是文件(非文件夹)

			response.setContentType("application/x-download");//设置为下载application/x-download
			String fileName = URLEncoder
					.encode(file.getName(), "UTF-8");
			fileName = fileName.replaceAll("[+]", "%20");//替换空格
			response.addHeader("Content-Disposition","attachment;filename*=UTF-8''" + fileName);
 
			OutputStream outputStream = response.getOutputStream();
			InputStream inputStream = new FileInputStream(strDir);
			byte[] buffer = new byte[1024];
			int i = -1;
			while ((i = inputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, i);
			}
			outputStream.flush();
			outputStream.close();
			inputStream.close();
			outputStream = null;

			out.clear();//不加这两句会报错~
			out = pageContext.pushBody();
		}
	}
%>
<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文件下载</title>

</head>
<body>
	<span style="color: red;"> <%=error%>
	</span>

</body>
</html>